const url = 'https://d5cdn.cdollar.cn/d5/app/dollar/c2cf32f6-519f-4697-a22b-971137d6d05e/release/app/entry.html?sharePermit&version=5.3.0&insideState&shareCallback'

// 1、URLSearchParams （存在兼容性问题）
function getUrlParams1(url){
  // 如果不分割？前面的，最后会多一个属性 url.split('?')[0]: ''
  const urlParamsStr = url.split('?')[1]
  const urlSearchParams = new URLSearchParams(urlParamsStr)
  /**
  URLSearchParams 
  {
    'sharePermit' => '',
    'version' => '5.3.0',
    'insideState' => '',
    'shareCallback' => '' 
  }
  [object URLSearchParams]
   */
  console.log(urlSearchParams, urlSearchParams.entries(), 'urlSearchParams', Object.prototype.toString.call(urlSearchParams));
  //  Object.entries把URLSearchParams对象转成key value的键值对列表（迭代协议iterator）  所以Object.fromEntries再把键值对列表转成对象
  const params = Object.fromEntries(urlSearchParams.entries())
  /**
 {
    sharePermit: '',
    version: '5.3.0',
    insideState: '',
    shareCallback: ''
  }
   */
  
  console.log(params, 'params1');
  
  return params
}


// 2、split
function getUrlParams2(url) {
  const urlParamsStr = url.split('?')[1]
  const params = {}
  const paramsArr = urlParamsStr.split('&')
  for (let i = 0; i < paramsArr.length; i++) {
    const paramsItem = paramsArr[i].split('=')
    params[paramsItem[0]] = paramsItem[1]
  } 
  console.log(params, 'params2');
  return params
}

// 3.正则
function getUrlParams3(url) {
  const pattern = /(\w+)=(\w+)/ig; //定义正则表达式
  const params = {}; // 定义参数对象
  url.replace(pattern, ($, $1, $2) => {
    params[$1] = $2
  })
  console.log(params, 'params3');
  return params
}

// 4、利用a标签的属性  第三方库..
getUrlParams1(url)
getUrlParams2(url)
getUrlParams3(url)